package com.bw.zhuangao5___day002.Fragment;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.CheckBox;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bw.mvp.V.BannerFragment;
import com.bw.zhuangao5___day002.Adpter.FragmentAdpter.FragmentAdpterTwo;
import com.bw.zhuangao5___day002.Fragment.Home.Fragment1.BlankFragmentHome1;
import com.bw.zhuangao5___day002.Fragment.Home.Fragment2.BlankFragmentHome2;
import com.bw.zhuangao5___day002.Fragment.Home.Fragment3.BlankFragmentHome3;
import com.bw.zhuangao5___day002.Fragment.Home.JavaBanner;
import com.bw.zhuangao5___day002.R;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;


public class BlankFragmentHome extends BannerFragment {

    List<JavaBanner.DataBean> dataBeans;
    private Banner xiaozi;
    private CheckBox HomeMeishi;
    private CheckBox HomeYule;
    private CheckBox HomeDiaoyu;
    private CheckBox HomeChengren;
    private TabLayout HomeTable;
    private ViewPager HomeViewPager;

    @Override
    public int bandLayoud() {
        return R.layout.fragment_blank_fragment_home;
    }

    @Override
    public void initView() {
        xiaozi = (Banner) findViewById(R.id.xiaozi);
        HomeMeishi = (CheckBox) findViewById(R.id.Home_meishi);
        HomeYule = (CheckBox) findViewById(R.id.Home_yule);
        HomeDiaoyu = (CheckBox) findViewById(R.id.Home_diaoyu);
        HomeChengren = (CheckBox) findViewById(R.id.Home_chengren);
        HomeTable = (TabLayout) findViewById(R.id.Home_table);
        HomeViewPager = (ViewPager) findViewById(R.id.Home_viewPager);
    }

    @Override
    public void initDate() {
        //---------------------------------------------------------------------------------------------------------------
        List<String> stringList1 = new ArrayList<>();
        stringList1.add("离我最近");
        stringList1.add("今日推荐");
        stringList1.add("爆款推荐");
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new BlankFragmentHome1());
        fragmentList.add(new BlankFragmentHome2());
        fragmentList.add(new BlankFragmentHome3());

        FragmentAdpterTwo two = new FragmentAdpterTwo(getActivity().getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,fragmentList,stringList1);
        HomeViewPager.setAdapter(two);
        HomeTable.setupWithViewPager(HomeViewPager);


        List<String> stringList = new ArrayList<>();
        OkGo.<String>get("https://www.wanandroid.com/banner/json").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Gson gson = new Gson();
                JavaBanner javaBanner = gson.fromJson(response.body().toString(), JavaBanner.class);
                dataBeans = javaBanner.getData();//------全局
                xiaozi.setImages(dataBeans);//------设置图片
                for (JavaBanner.DataBean list : dataBeans) {
                    stringList.add(list.getTitle().toString());//------设置文字
                }
                xiaozi.setBannerTitles(stringList);//------设置文字
                xiaozi.setBannerAnimation(Transformer.ScaleInOut);//------设置动画效果
                xiaozi.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);//------类型
                xiaozi.setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        JavaBanner.DataBean dataBean = (JavaBanner.DataBean) path;
                        Glide.with(getActivity()).load(dataBean.getImagePath()).into(imageView);//------设置解析图片
                    }
                }).start();
            }
        });

        //----------------设置banner每一个图片监听事件
        xiaozi.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Uri uri = Uri.parse("" + dataBeans.get(position).getUrl());
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });




    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().finish();
    }
}
